package com.geekq.miaosha.redis.redismanager;

import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.geekq.miaosha.redis.redismanager.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Transaction;

/**
 * redis乐观锁实例
 * @author linbingwen
 *
 */
public class OptimisticLockTest {
	public static void main(String[] args) throws InterruptedException {
		long starTime=System.currentTimeMillis();

		initPrduct();
		initClient();
		printResult();

		long endTime=System.currentTimeMillis();
		long Time=endTime-starTime;
		System.out.println("程序运行时间： "+Time+"ms");

	}

	/**
	 * 输出结果
	 */
	public static void printResult() {
		Jedis jedis = RedisUtil.getInstance().getJedis();
		Set<String> set = jedis.smembers("clientList");

		int i = 1;
		for (String value : set) {
			System.out.println("第" + i++ + "个抢到商品，"+value + " ");
		}

		RedisUtil.returnResource(jedis);
	}

	/*
	 * 初始化顾客开始抢商品
	 */
	public static void initClient() {
		ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
		int clientNum = 10000;// 模拟客户数目
		for (int i = 0; i < clientNum; i++) {
			cachedThreadPool.execute(new ClientThread(i));
		}
		cachedThreadPool.shutdown();

		while(true){
			if(cachedThreadPool.isTerminated()){
				System.out.println("所有的线程都结束了！");
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 初始化商品个数
	 */
	public static void initPrduct() {
		int prdNum = 100;// 商品个数
		String key = "prdNum";
		String clientList = "clientList";// 抢购到商品的顾客列表
		Jedis jedis = RedisUtil.getInstance().getJedis();

		if (jedis.exists(key)) {
			jedis.del(key);
		}

		if (jedis.exists(clientList)) {
			jedis.del(clientList);
		}

		jedis.set(key, String.valueOf(prdNum));// 初始化
		RedisUtil.returnResource(jedis);
	}

}

/**
 * 顾客线程
 *
 * @author linbingwen
 *
 */
class ClientThread implements Runnable {
	Jedis jedis = null;
	String key = "prdNum";// 商品主键
	String clientList = "clientList";//// 抢购到商品的顾客列表主键
	String clientName;

	public ClientThread(int num) {
		clientName = "编号=" + num;
	}


	public void run() {
		try {
			Thread.sleep((int)(Math.random()*5000));// 随机睡眠一下
		} catch (InterruptedException e1) {
		}
		while (true) {
			System.out.println("顾客:" + clientName + "开始抢商品");
			jedis = RedisUtil.getInstance().getJedis();
			try {
				jedis.watch(key);
				int prdNum = Integer.parseInt(jedis.get(key));// 当前商品个数
				if (prdNum > 0) {
					Transaction transaction = jedis.multi();
					transaction.set(key, String.valueOf(prdNum - 1));
					List<Object> result = transaction.exec();
					if (result == null || result.isEmpty()) {
						System.out.println("悲剧了，顾客:" + clientName + "没有抢到商品");// 可能是watch-key被外部修改，或者是数据操作被驳回
					} else {
						jedis.sadd(clientList, clientName);// 抢到商品记录一下
						System.out.println("好高兴，顾客:" + clientName + "抢到商品");
						break;
					}
				} else {
					System.out.println("悲剧了，库存为0，顾客:" + clientName + "没有抢到商品");
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				jedis.unwatch();
				RedisUtil.returnResource(jedis);
			}

		}
	}

}
